﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Shmup_Project
{
    public class Ship : AnimatedSprite
    {
       public float speed = 500;
       const int SHIPHEIGHT = 50;
       const int SHIPWIDTH = 59;
       public int playerHealth = 150;
       public const int MAX_HEALTH = 150 ;

       SpriteAnimation movingAnimation = new SpriteAnimation
       {
           blooping = true,
           columns = 2,
           drawPosition = new Vector2(),
           frameHeight = 50,
           frameLength = 2,
           frameWidth = 59,
           rows = 1,
           totalFrames = 2
       };
       SpriteAnimation explosionAnimation = new SpriteAnimation()
       {
           blooping = false,
           columns = 12,
           rows = 1,
           drawPosition = Vector2.Zero,
           frameHeight = 134,
           frameWidth = 133,
           totalFrames = 12
       };

       public Ship(Game1 gameClass, int xPosition, int yPosition) : base (gameClass,xPosition,yPosition, SHIPHEIGHT, SHIPWIDTH)
       {
           objectType = ObjectType.PlayerShip;
       }

       public override void Initialize()
       {
           currentAnimation = movingAnimation;
           base.Initialize();
       }
       public override void LoadContent()
       {
           texture = gameClass.Content.Load < Texture2D > ("spritesheet");
           base.LoadContent();
       }
       public override void Update(GameTime gameTime)
       {
           if (bDestroyed && currentFrame >= currentAnimation.totalFrames - 1)
               gameClass.Lose();
           base.Update(gameTime);
       }

       public override void HitByEnemyType(WorldObject hitByObject)
       {
           base.HitByEnemyType(hitByObject);
           playerHealth -= ((Enemy)hitByObject).Damage;
           CheckHealth();
       }

       public override void HitByEnemyProjectileType(WorldObject hitByObject)
       {
           base.HitByEnemyProjectileType(hitByObject);
           playerHealth -= ((EnemyProjectile)hitByObject).damage;
           CheckHealth();
       }

       private void CheckHealth()
       {
           if (playerHealth <= 0)
               PlayerDies();
       }

       public void PlayerDies()
       {
           BlowUpShip();
       }

       public void BlowUpShip()
       {
           bCollidable = false;
           bDestroyed = true;
           texture = gameClass.Content.Load<Texture2D>("explosion");
           currentAnimation = explosionAnimation;
       }

       public void MoveShip(Vector2 thumbstickMovement, GameTime gameTime)
       {
        position.X += (speed * thumbstickMovement.X) * (float)gameTime.ElapsedGameTime.TotalSeconds;
        position.Y += (speed * -thumbstickMovement.Y) * (float)gameTime.ElapsedGameTime.TotalSeconds;
        if (position.Y < 0)
        {
            position.Y = 0;
        }
        if (position.Y+boundingBox.Height > gameClass.GraphicsDevice.Viewport.Height)
        {
            position.Y = gameClass.GraphicsDevice.Viewport.Height - boundingBox.Height;
        }

        if (position.X < 0)
            position.X = 0;

        if (position.X + boundingBox.Width > gameClass.GraphicsDevice.Viewport.Width)
            position.X = gameClass.GraphicsDevice.Viewport.Width-boundingBox.Width;
       
       }
       
    }
}
